package coms.dataaccess.hibernate;

import coms.dataaccess.dao.ConferenceRegistrationDAO;
import coms.dto.Conference;
import coms.dto.ConferenceRegistration;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

public class ConferenceRegistrationDAOImpl extends GenericDAOImpl<ConferenceRegistration, Integer> implements ConferenceRegistrationDAO {

    private Session session;

    /*
     * @Override public List<ConferenceRegistration> searchByConference
     * (Conference conference){ session =
     * HibernateUtil.getSessionFactory().openSession(); Criteria criteria =
     * session.createCriteria(Conference.class); if (conference.getName() !=
     * null) { criteria.add(Restrictions.eq("conference.id",
     * conference.getId())); }
     *
     * return criteria.list(); }
     */
    @Override
    public List<ConferenceRegistration> search(ConferenceRegistration conferenceRegistration) throws SQLException {
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(ConferenceRegistration.class, "confReg");
        if (conferenceRegistration == null) {
            return criteria.list();
        }

        if (conferenceRegistration.getConference() != null) {
            criteria.createAlias("confReg.conference", "conf").add(Restrictions.eq("conf.id", conferenceRegistration.getConference().getId()));
        }
        if(conferenceRegistration.isValid()==true)
        {
            criteria.add(Restrictions.eq("valid",true));
           
        }
        if (conferenceRegistration.getParticipant() != null) {
            criteria.createAlias("confReg.participant", "p").add(Restrictions.eq("p.id", conferenceRegistration.getParticipant().getId()));
        }
        return criteria.list();
    }
}
